import {
  ElMessageBox,
  type ElMessageBoxOptions,
  type MessageBoxData
} from 'element-plus'

import { transformI18n } from '@/plugins/i18n'

/**
 * 自定义 confirm 对话框
 * 统一样式和默认的 cancelText 和 confirmText
 * @param message - 对话框消息内容
 * @param title - 对话框标题，默认为 '提示'
 * @param options - 其他 MessageBox 的配置项，可覆盖默认值
 * @returns Promise 对象，resolve 表示确认，reject 表示取消
 */
export function Confirm(
  message: ElMessageBoxOptions['message'],
  title?: ElMessageBoxOptions['title'],
  options?: Partial<ElMessageBoxOptions>
): Promise<MessageBoxData>

export function Confirm(
  message: ElMessageBoxOptions['message'],
  options?: Partial<ElMessageBoxOptions>
): Promise<MessageBoxData>

export function Confirm(
  message: string,
  titleOrOptions: string | Partial<ElMessageBoxOptions>,
  options?: Partial<ElMessageBoxOptions>
): Promise<MessageBoxData> {
  let title: string
  let opts: Partial<ElMessageBoxOptions>

  if (typeof titleOrOptions === 'string') {
    title = titleOrOptions
    opts = options || {}
  } else {
    title = transformI18n('button.promptText')
    opts = titleOrOptions
  }

  const defaultOptions: ElMessageBoxOptions = {
    confirmButtonText: transformI18n('button.confirmText'),
    cancelButtonText: transformI18n('button.cancelText'),
    type: 'warning',
    customClass: 'easy-message-box',
    ...opts
  }

  return ElMessageBox.confirm(message, title, defaultOptions)
}
